﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cinch;
using System.ComponentModel;
using System.ComponentModel.Composition;

namespace Anata.Client
{
    /// <summary>
    /// Represents a single Image ViewModel
    /// </summary>
    public class AsetViewModel : EditableValidatingViewModelBase
    {
        #region Data

        private ViewMode currentViewMode = ViewMode.AddMode;

        #region Data.Primitives
        private DataWrapper<Int32> idAset;
        private DataWrapper<Int32?> idJenisAset;
        private String namaJenisAset;
        private DataWrapper<Int32?> idCabang;
        private String namaCabang;
        private DataWrapper<String> nama;
        private DataWrapper<Int32> hargaBeli;
        private DataWrapper<DateTime?> waktuBeli;
        private DataWrapper<Int32> hargaJual;
        private DataWrapper<DateTime?> waktuJual;
        private DataWrapper<DateTime?> tanggalMortisasi;

        private bool isBusy = false;
        #endregion

        private IEnumerable<DataWrapperBase> cachedListOfDataWrappers;

        #region Data.Rules
        #endregion

        #region Data.VMs
        #endregion

        #region Data.Services
        private IMessageBoxService messageBoxService;
        private IUIVisualizerService uiVisualizerService;
        private IReferenceProvider referenceProvider;
        #endregion

        #endregion

        #region Constructor
        [ImportingConstructor]
        public AsetViewModel(
            IMessageBoxService messageBoxService,
            IUIVisualizerService uiVisualizerService,
            IReferenceProvider referenceProvider)
        {
            #region Constructor.SetupServices
            this.messageBoxService = messageBoxService;
            this.uiVisualizerService = uiVisualizerService;
            this.referenceProvider = referenceProvider;
            #endregion

            #region Constructor.SetupCommands
            CabangSelectCommand = new SimpleCommand<Object, Object>(CanExecuteCabangSelectCommand, ExecuteCabangSelectCommand);
            JenisAsetSelectCommand = new SimpleCommand<Object, Object>(CanExecuteJenisAsetSelectCommand, ExecuteJenisAsetSelectCommand);
            AsetSellCommand = new SimpleCommand<Object, Object>(CanExecuteAsetSellCommand, ExecuteAsetSellCommand);
            AsetDeleteCommand = new SimpleCommand<Object, Object>(CanExecuteAsetDeleteCommand, ExecuteAsetDeleteCommand);
            #endregion

            #region Constructor.CreateDataWrappers
            IdAset = new DataWrapper<Int32>(this, idAsetChangeArgs);
            IdAset.IsEditable = true;
            IdJenisAset = new DataWrapper<Int32?>(this, idJenisAsetChangeArgs);
            IdJenisAset.IsEditable = true;
            IdCabang = new DataWrapper<Int32?>(this, idCabangChangeArgs);
            IdCabang.IsEditable = true;
            Nama = new DataWrapper<String>(this, namaChangeArgs);
            Nama.IsEditable = true;
            HargaBeli = new DataWrapper<Int32>(this, hargaBeliChangeArgs);
            HargaBeli.IsEditable = true;
            WaktuBeli = new DataWrapper<DateTime?>(this, waktuBeliChangeArgs);
            WaktuBeli.IsEditable = true;
            HargaJual = new DataWrapper<Int32>(this, hargaJualChangeArgs);
            HargaJual.IsEditable = true;
            WaktuJual = new DataWrapper<DateTime?>(this, waktuJualChangeArgs);
            WaktuJual.IsEditable = true;
            TanggalMortisasi = new DataWrapper<DateTime?>(this, tanggalMortisasiChangeArgs);
            TanggalMortisasi.IsEditable = true;
            #endregion

            //fetch list of all DataWrappers, so they can be used again later without the
            //need for reflection
            cachedListOfDataWrappers =
                DataWrapperHelper.GetWrapperProperties<AsetViewModel>(this);

            #region Create Validation Rules
            #endregion

            #region Constructor.SetupCollections
            #endregion
        }
        #endregion

        #region Static
        static AsetViewModel()
        {
        }
        #endregion

        #region Public Properties

        #region Public.Commands
        public SimpleCommand<Object, Object> JenisAsetSelectCommand { get; private set; }
        public SimpleCommand<Object, Object> CabangSelectCommand { get; private set; }
        public SimpleCommand<Object, Object> AsetSellCommand { get; private set; }
        public SimpleCommand<Object, Object> AsetDeleteCommand { get; private set; }
        #endregion

        #region Public.ViewMode
        /// <summary>
        /// The current ViewMode, when changed will loop
        /// through all nested DataWrapper objects and change
        /// their state also
        /// </summary>

        static PropertyChangedEventArgs currentViewModeChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.CurrentViewMode);

        public ViewMode CurrentViewMode
        {
            get { return currentViewMode; }
            set
            {
                currentViewMode = value;

                switch (currentViewMode)
                {
                    case ViewMode.AddMode:
                        //Quantity.DataValue= 0;
                        this.DisplayName = "Transaksi Baru";
                        break;
                    case ViewMode.EditMode:
                        this.DisplayName = "Ubah Transaksi";
                        break;
                    case ViewMode.ViewOnlyMode:
                        this.DisplayName = "Lihat Transaksi";
                        break;
                }

                //Now change all the CachedListOfDataWrappers
                //Which sets all the Cinch.DataWrapper<T>s to the correct IsEditable
                //state based on the new ViewMode applied to the ViewModel
                //we can use the Cinch.DataWrapperHelper class for this
                DataWrapperHelper.SetMode(
                    CachedListOfDataWrappers,
                    currentViewMode);

                NotifyPropertyChanged(currentViewModeChangeArgs);
            }
        }
        #endregion

        #region Public.Data
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs idAsetChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.IdAset);
        public DataWrapper<Int32> IdAset
        {
            get { return idAset; }
            set
            {
                idAset = value;
                NotifyPropertyChanged(idAsetChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs idJenisAsetChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.IdJenisAset);
        public DataWrapper<Int32?> IdJenisAset
        {
            get { return idJenisAset; }
            set
            {
                idJenisAset = value;
                NotifyPropertyChanged(idJenisAsetChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs namaJenisAsetChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.NamaJenisAset);
        public String NamaJenisAset
        {
            get { return namaJenisAset; }
            set
            {
                namaJenisAset = value;
                NotifyPropertyChanged(namaJenisAsetChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs idCabangChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.IdCabang);
        public DataWrapper<Int32?> IdCabang
        {
            get { return idCabang; }
            set
            {
                idCabang = value;
                NotifyPropertyChanged(idCabangChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs namaCabangChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.NamaCabang);
        public String NamaCabang
        {
            get { return namaCabang; }
            set
            {
                namaCabang = value;
                NotifyPropertyChanged(namaCabangChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs namaChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.Nama);
        public DataWrapper<String> Nama
        {
            get { return nama; }
            set
            {
                nama = value;
                NotifyPropertyChanged(namaChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs hargaBeliChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.HargaBeli);
        public DataWrapper<Int32> HargaBeli
        {
            get { return hargaBeli; }
            set
            {
                hargaBeli = value;
                NotifyPropertyChanged(hargaBeliChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs waktuBeliChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.WaktuBeli);
        public DataWrapper<DateTime?> WaktuBeli
        {
            get { return waktuBeli; }
            set
            {
                waktuBeli = value;
                NotifyPropertyChanged(waktuBeliChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs hargaJualChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.HargaJual);
        public DataWrapper<Int32> HargaJual
        {
            get { return hargaJual; }
            set
            {
                hargaJual = value;
                NotifyPropertyChanged(hargaJualChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs waktuJualChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.WaktuJual);
        public DataWrapper<DateTime?> WaktuJual
        {
            get { return waktuJual; }
            set
            {
                waktuJual = value;
                NotifyPropertyChanged(waktuJualChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs tanggalMortisasiChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.TanggalMortisasi);
        public DataWrapper<DateTime?> TanggalMortisasi
        {
            get { return tanggalMortisasi; }
            set
            {
                tanggalMortisasi = value;
                NotifyPropertyChanged(tanggalMortisasiChangeArgs);
            }
        }
        #endregion

        #region Public.IsBusy
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs isBusyChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.IsBusy);
        public bool IsBusy
        {
            get { return isBusy; }
            set
            {
                isBusy = value;
                NotifyPropertyChanged(isBusyChangeArgs);
            }
        }
        #endregion

        #region Public.Cached Collection
        /// <summary>
        /// Returns cached collection of DataWrapperBase
        /// </summary>
        public IEnumerable<DataWrapperBase> CachedListOfDataWrappers
        {
            get { return cachedListOfDataWrappers; }
        }
        #endregion

        #region Public.DataVM
        #endregion

        #region Public.SummaryData
        #endregion

        #endregion

        #region Command Handlers
        /// <summary>
        /// </summary>
        private bool CanExecuteCabangSelectCommand(Object args)
        {
            return !isBusy && CurrentViewMode != ViewMode.ViewOnlyMode;
        }
        /// <summary>
        /// </summary>
        private void ExecuteCabangSelectCommand(Object args)
        {
            //CabangSelectViewModel csvm = new CabangSelectViewModel(
            //    messageBoxService,
            //    uiVisualizerService,
            //    referenceProvider);
            //bool? result = uiVisualizerService.ShowDialog("CabangSelectPopup", csvm);
            //if (result.HasValue && result.Value)
            //{
            //    CabangThumbnailViewModel ctvm = (CabangThumbnailViewModel)
            //        csvm.CabangsCV.CurrentItem;
            //    this.IdCabang.DataValue = ctvm.IdCabang;
            //    this.NamaCabang = ctvm.Nama;
            //}
        }

        /// <summary>
        /// </summary>
        private bool CanExecuteJenisAsetSelectCommand(Object args)
        {
            return !isBusy && CurrentViewMode != ViewMode.ViewOnlyMode;
        }
        /// <summary>
        /// </summary>
        private void ExecuteJenisAsetSelectCommand(Object args)
        {
            //JenisAsetSelectViewModel jasvm = new JenisAsetSelectViewModel(
            //    messageBoxService,
            //    uiVisualizerService,
            //    referenceProvider);
            //bool? result = uiVisualizerService.ShowDialog("JenisAsetSelectPopup", jasvm);
            //if (result.HasValue && result.Value)
            //{
            //    JenisAsetViewModel javm = (JenisAsetViewModel)
            //        jasvm.JenisStoksCV.CurrentItem;
            //    this.IdJenisAset.DataValue = javm.IdJenisAset;
            //    this.NamaJenisAset = javm.Nama;
            //}
        }

        /// <summary>
        /// </summary>
        private bool CanExecuteAsetSellCommand(Object args)
        {
            return !isBusy && CurrentViewMode != ViewMode.ViewOnlyMode;
        }
        /// <summary>
        /// </summary>
        private void ExecuteAsetSellCommand(Object args)
        {
            //Mediator.Instance.NotifyColleagues("TryRemoveKaryawanLayanan", this);
        }

        /// <summary>
        /// </summary>
        private bool CanExecuteAsetDeleteCommand(Object args)
        {
            return !isBusy && CurrentViewMode != ViewMode.ViewOnlyMode;
        }
        /// <summary>
        /// </summary>
        private void ExecuteAsetDeleteCommand(Object args)
        {
            Mediator.Instance.NotifyColleagues("TryRemoveAset", this);
        }
        #endregion

        #region Change Callbacks
        #endregion

        #region Private Methods
        #endregion

        #region Overrides
        static PropertyChangedEventArgs isValidChangeArgs =
            ObservableHelper.CreateArgs<AsetViewModel>(x => x.IsValid);

        public override bool IsValid
        {
            get
            {
                //return base.IsValid and use DataWrapperHelper, if you are
                //using DataWrappers
                return base.IsValid &&
                    DataWrapperHelper.AllValid(cachedListOfDataWrappers);
            }

        }

        public bool IsDirty
        {
            get
            {
                return DataWrapperHelper2.SomeDirty(cachedListOfDataWrappers);
            }
        }

        public override string ToString()
        {
            return String.Format("AsetViewModel {0}", this.idAset);
        }
        #endregion

        #region EditableValidatingViewModelBase overrides
        protected override void OnBeginEdit()
        {
            base.OnBeginEdit();
            //Now walk the list of properties in the OrderViewModel
            //and call BeginEdit() on all Cinch.DataWrapper<T>s.
            //we can use the Cinch.DataWrapperHelper class for this
            DataWrapperHelper.SetBeginEdit(cachedListOfDataWrappers);
        }

        /// <summary>
        /// Override hook which allows us to also put any child 
        /// EditableValidatingViewModelBase objects into the EndEdit state
        /// </summary>
        protected override void OnEndEdit()
        {
            base.OnEndEdit();
            //Now walk the list of properties in the OrderViewModel
            //and call CancelEdit() on all Cinch.DataWrapper<T>s.
            //we can use the Cinch.DataWrapperHelper class for this
            DataWrapperHelper.SetEndEdit(cachedListOfDataWrappers);
        }

        /// <summary>
        /// Override hook which allows us to also put any child 
        /// EditableValidatingViewModelBase objects into the CancelEdit state
        /// </summary>
        protected override void OnCancelEdit()
        {
            base.OnCancelEdit();
            //Now walk the list of properties in the OrderViewModel
            //and call CancelEdit() on all Cinch.DataWrapper<T>s.
            //we can use the Cinch.DataWrapperHelper class for this
            DataWrapperHelper.SetCancelEdit(cachedListOfDataWrappers);
        }
        #endregion
    }
}
